# Inputs:
#   RobustnessLeaveOneOut_Reform_beliefs.csv
#   RobustnessLeaveOneOut_Reform_behaviors.csv
#   RobustnessLeaveOneOut_Reform_policy.csv
#   RobustnessLeaveOneOut_Reform_voting.csv	
  # 
  # RobustnessLeaveOneOut_Country_beliefs.csv
  # RobustnessLeaveOneOut_Country_behaviors.csv
  # RobustnessLeaveOneOut_Country_policy.csv
  # RobustnessLeaveOneOut_Country_voting.csv
# Outputs:
#   Appendix Figure A3

library(dplyr)
library(ggplot2)



data_beliefs_reform <- "RobustnessLeaveOneOut_Reform_beliefs.csv" %>% read.csv2(sep=",")
data_beliefs_country <- "RobustnessLeaveOneOut_Country_beliefs.csv" %>% read.csv2(sep=",")
beliefs <- rbind(
  data.frame(val=data_beliefs_reform[3,] %>% substring(2) %>% as.numeric(), lab="Reform"),
  data.frame(val=data_beliefs_country[3,] %>% substring(2) %>% as.numeric(), lab="Country")
)

data_behaviors_reform <- "RobustnessLeaveOneOut_Reform_behaviors.csv" %>% read.csv2(sep=",")
data_behaviors_country <- "RobustnessLeaveOneOut_Country_behaviors.csv" %>% read.csv2(sep=",")
behaviors <- rbind(
  data.frame(val=data_behaviors_reform[3,] %>% substring(2) %>% as.numeric(), lab="Reform"),
  data.frame(val=data_behaviors_country[3,] %>% substring(2) %>% as.numeric(), lab="Country")
)

data_policy_reform <- "RobustnessLeaveOneOut_Reform_policy.csv" %>% read.csv2(sep=",")
data_policy_country <- "RobustnessLeaveOneOut_Country_policy.csv" %>% read.csv2(sep=",")
policy <- rbind(
  data.frame(val=data_policy_reform[3,] %>% substring(2) %>% as.numeric(), lab="Reform"),
  data.frame(val=data_policy_country[3,] %>% substring(2) %>% as.numeric(), lab="Country")
)

data_voting_reform <- "RobustnessLeaveOneOut_Reform_voting.csv" %>% read.csv2(sep=",")
data_voting_country <- "RobustnessLeaveOneOut_Country_voting.csv" %>% read.csv2(sep=",")
voting <- rbind(
  data.frame(val=data_voting_reform[3,] %>% substring(2) %>% as.numeric(), lab="Reform"),
  data.frame(val=data_voting_country[3,] %>% substring(2) %>% as.numeric(), lab="Country")
)

data <- rbind(
  beliefs %>% mutate(var="Beliefs"),
  behaviors %>% mutate(var="Behaviors"),
  policy %>% mutate(var="Policy"),
  voting %>% mutate(var="Voting")
)




ggplot(behaviors, aes(x=val, color=lab)) +
  geom_histogram(aes(y=..count../sum(..count..)),fill="white", alpha=0.1, position="identity") +
  labs(title="Behaviors", x="β", y="Proportion")+guides(color=guide_legend(title="Leave One Out Type")) + theme_classic()



ggplot(data %>% filter(!is.na(val)), aes(x=val, color=lab)) +
  geom_histogram(aes(y=..count../sum(..count..)),fill="white", alpha=0.1, position="identity") + facet_grid(.~var)+
  labs(title="Behaviors", x="β", y="Proportion")+guides(color=guide_legend(title="Leave One Out Type")) + theme_classic()


plot <- ggplot(data %>% filter(!is.na(val)), aes(x=val, fill=lab), color="black") +
  geom_histogram(data=data %>% filter(!is.na(val)&lab=="Country"), aes(y=..count../sum(..count..)), alpha=0.2, position="identity") + 
  geom_histogram(data=data %>% filter(!is.na(val)&lab=="Reform"), aes(y=..count../sum(..count..)), alpha=0.2, position="identity") +
  facet_grid(.~var)+
  labs(title="Behaviors", x="β", y="Proportion")+guides(fill=guide_legend(title="Leave One Out Type")) + theme_classic()
plot


ggsave( "LeaveOneOutHistograms.png" , plot = plot, device = "png", width=10, height=4)  

hist2 <- ggplot(data %>% filter(!is.na(val) & lab=="Country"), aes(x=val), fill="white", color="black") +
  geom_histogram(aes(y=..count../sum(..count..)), alpha=0.2, position="identity")+geom_histogram(data=data %>% filter(!is.na(val) & lab=="Reform"), aes(y=..count../sum(..count..)), alpha=0.2, position="identity") + facet_grid(.~var)+
  labs(title="Behaviors", x="β", y="Density")+guides(fill=guide_legend(title="Leave One Out Type")) + theme_classic()
hist2



# Boxplot
boxplot <- ggplot(data %>% filter(!is.na(val)), aes(x=val), fill="white", color="black") +
  geom_boxplot( alpha=0.2, position="identity") + facet_grid(lab~var)+
  labs(title="", x="β")  +theme_classic() + theme(axis.text.y=element_blank(),  axis.ticks.y=element_blank())
boxplot

ggsave("LeaveOneOutBoxplots.png" , plot = boxplot, device = "png", width=10, height=4)  
